/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.common.io;

import static org.sss.common.Utils.wrap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sss.common.DataReceiver;
import org.sss.common.KeyValues;
import org.sss.mapreduce.Configuration;
import org.sss.mapreduce.DB;
import org.sss.mapreduce.GroupID;
import org.sss.mapreduce.SssException;
import org.sss.mapreduce.StorageNode;
import org.sss.util.BytesComparator;

public class WholeDataKeyValueReader extends AbstractKeyValueReader {
  private static Logger logger = LoggerFactory.getLogger(WholeDataKeyValueReader.class);
  private static BytesComparator CMP = new BytesComparator();

  public WholeDataKeyValueReader(Configuration conf,
                                 StorageNode node,
                                 GroupID source,
                                 int subhash,
                                 DataReceiver<List<KeyValues>> next) throws SssException {
    super(conf, node, source, subhash, next);
  }

  @Override
  protected void doRead(IOThread iot)
    throws SssException, IOException, InterruptedException {
    assert getSource() != null: "source should not be null";
    DB db = iot.getDB(node);
    int limitNBytes = iot.getIOGetLimitNBytes();

    final KeyFormatter keyFormatter = KeyFormatter.get(getSource().getKeyFormat());
    assert keyFormatter == KeyFormatter.processingOrderFormatter: "KeyFormatter is not processingOrderFormatter.";
    boolean binc = true;
    boolean done = false;
    byte[]       prefix   = KeyFormatter.getPrefix(getSource(), subhash);
    final byte[] sentinel = KeyFormatter.getSentinel(getSource(), subhash);
    final Map<byte[], List<byte[]>> multimap = new TreeMap<byte[], List<byte[]>>(CMP);
    while (!done) {
      checkCanceled();
      List<byte[]> kvs = db.range(prefix, sentinel, limitNBytes, binc);
      countup(kvs);
      final int count = kvs.size();
      if (isDestructiveRead() && count > 0) {
        db.rangeout(prefix, sentinel, count / 2, binc);
      }
      binc = false;
      if (count == 0) {
        break;
      }
      prefix = kvs.get(count - 2);
      logger.debug("count = {}", count);
      for (int i=0; i<count; i+=2) {
        byte[] userkey = keyFormatter.getUserKey(kvs.get(i));
        List<byte[]> vs = multimap.get(userkey);
        if (vs == null) {
          vs = new ArrayList<byte[]>();
          multimap.put(userkey, vs);
        }
        vs.add(kvs.get(i + 1));
      }
    }
    for (byte[] key: multimap.keySet()) {
      next.put(Arrays.asList(new KeyValues(key, wrap(multimap.get(key).iterator()))));
    }
  }
}
